package com.hp.utils;


import cn.hutool.core.collection.CollUtil;
import com.hp.entity.request.AlarmContentRequest;
import com.hp.entity.request.ContentRequest;
import com.hp.entity.request.content.PostContent;
import com.hp.entity.request.content.post.Content;
import com.hp.entity.request.content.post.Post;
import com.hp.entity.request.content.post.PostElement;
import com.hp.enums.PostTagEnum;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hp
 */
@Component
public class LarkRobotUtils {


    public static String genSign(String secret, int timestamp) throws NoSuchAlgorithmException, InvalidKeyException {
        //把timestamp+"\n"+密钥当做签名字符串
        String stringToSign = timestamp + "\n" + secret;

        //使用HmacSHA256算法计算签名
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(stringToSign.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
        byte[] signData = mac.doFinal(new byte[]{});
        return new String(Base64.encodeBase64(signData));
    }


    /**
     * 富文本 post类型消息
     */
    public static PostContent getPostContent(AlarmContentRequest request) {

        // 富文本内容
        PostContent postContent = new PostContent();
        // 创建内容并封装
        List<List<PostElement>> postElements = new ArrayList<>();
        // 处理内容
        handleContentList(postElements, request.getContentList());
        // 拼装结果内容
        setPostContent(request, postElements, postContent);

        return postContent;
    }

    private static void setPostContent(AlarmContentRequest request, List<List<PostElement>> postElements, PostContent postContent) {
        Content cnContent = new Content();
        cnContent.setTitle(request.getTitle());
        cnContent.setPostElements(postElements);

        Post post = new Post();
        post.setCnContent(cnContent);

        postContent.setPost(post);
    }

    @SuppressWarnings("all")
    private static void handleContentList(List<List<PostElement>> contentList, List<ContentRequest> list) {
        // @所有人 + 标题
        contentList.add(List.of(createPostElement(PostTagEnum.AT, "all", null, null)));
        if (CollUtil.isEmpty(list)) return;
        // 遍历内容
        for (ContentRequest contentRequest : list) {
            // 创建元素列表
            List<PostElement> elements = new ArrayList<>();
            // 文本内容
            String text = contentRequest.getText();
            if (StringUtils.isNotBlank(text)) {
                elements.add(createPostElement(PostTagEnum.TEXT, null, text, null));
            }
            // 链接
            String href = contentRequest.getHref();
            if (StringUtils.isNotBlank(href)) {
                elements.add(createPostElement(PostTagEnum.LINK, null, contentRequest.getHrefDesc(), href));
            }
            contentList.add(elements);
        }
    }

    /**
     * 创建 PostElement
     */
    @SuppressWarnings("all")
    private static PostElement createPostElement(PostTagEnum tag, String userId, String text, String href) {
        PostElement element = new PostElement();
        element.setTag(tag.getTag());
        element.setUserId(userId);
        element.setText(text);
        element.setHref(href);
        return element;
    }

}
